<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/9/2 0002
 * Time: 上午 12:17
 */
namespace app\dlc\common\createHelper;
class Common
{

    public $str;
    public $add_file = [];
    public $end_action = [];//在类结束之前，要加载一些方法
    public function _initialize()
    {
        define("MYSQL_MODULE", config('default_module').'_');
    }
    public function save($file,$cover=true,$model=false){//如果是模型的话  $model = true
        $res = file_exists($file);
//        if ($res && !$cover){//$cover = fales  不允许覆盖
//            return ;
//        }
        $fp=fopen($file,"w"); //写方式打开文件
        fwrite($fp,$this->str); //存入内容
        fclose($fp); //关闭文件
        if ($model){
            foreach ($this->add_file as $key => $item) {
                $obj = new CreateModel();
                $obj->create($item,false);
            }
        }


        
    }


    static function getTableInfo($table,$type = 'fields',$key_copy_key = true){//type  获取信息类型 包括 fields type bind pk
        try{
            $default_module = config('default_module').'_';
            $fields =  \db()->getTableInfo($default_module.$table,$type);
            if ($key_copy_key&&$type='fields'&&is_array($fields)){
                $data = [];
                foreach ($fields as $key => $field) {
                    $data[$field] = $field;
                }
                return $data;
            }else{
                return $fields;
            }

        }catch (\Exception $e){
//            echo $table.'表不存在';
//            die;
            return false;
        }
    }

    //phh开头
    public function phpStart(){
        $this->str .= "<?php\r\n"; //得到php的起始符。$str_tmp将累加
        $this->str .= "/**\r\n";
        $this->str .= " * Maker: 梁康宁\r\n";
        $this->str .= " * Time: ".date('Y-m-d H:i:s')."\r\n";
        $this->str .= "*/\r\n";
    }
    //phh开头
    public function phpEnd(){
        $this->str .= "?>"; //得到php的起始符。$str_tmp将累加
    }

    //命名空间
    public function space($str){
        $this->str .= $str."\r\n";
    }

    public function classNameStart($str){
        $this->str .= $str."\r\n"."{\r\n";
    }

    public function classNameEnd(){
        foreach ($this->end_action as $key => $item) {
            if (method_exists($this,$item)){
                $this->$item();
            }
        }
        $this->str .= "}\r\n";
    }

    public function actionStart($str){
        $this->str .= "\t".$str."\r\n"."\t{\r\n";
    }

    public function actionEnd(){
        $this->str .= "\t"."}\r\n";
        $this->str .= "\r\n";
        $this->str .= "\r\n";
    }

    public function content($str){
        $this->str .= "\t\t".$str."\r\n";
    }

    /**
    　　* 下划线转驼峰
    　　* 思路:
    　　* step1.原字符串转小写,原字符串中的分隔符用空格替换,在字符串开头加上分隔符
    　　* step2.将字符串中每个单词的首字母转换为大写,再去空格,去字符串首部附加的分隔符.
    　　*/
    function camelize($uncamelized_words,$first=false,$separator='_')
    {
        $uncamelized_words = $separator. str_replace($separator, " ", strtolower($uncamelized_words));
        $data =  ltrim(str_replace(" ", "", ucwords($uncamelized_words)), $separator );
        if ($first){
            $data = ucfirst($data);
        }
        return $data;
    }

    /**
    　* 驼峰命名转下划线命名
    　　* 思路:
    　　* 小写和大写紧挨一起的地方,加上分隔符,然后全部转小写
    　　*/
    function uncamelize($camelCaps,$separator='_')
    {
        return strtolower(preg_replace('/([a-z])([A-Z])/', "$1" . $separator . "$2", $camelCaps));
    }

}